<template>
  <div class="goods">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="10">
        <el-col :span="6">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            class="input-with-select"
            clearable
            @clear="getTableData()"
          >
            <el-button slot="append" icon="el-icon-search" @click="getTableData()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="addCommodity()">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="tableData" stripe border style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="100px"></el-table-column>
        <el-table-column prop="goods_number" label="商品数量" width="100px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px"></el-table-column>
        <el-table-column label="创建时间" width="160px">
          <template slot-scope="scope">{{scope.row.add_time | timefilter}}</template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editCommodity(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteByid(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <el-dialog title="修改商品" :visible.sync="CommodityVisible" width="60%" @close="closeShop()">
      <el-form :model="shopForm" :rules="shopRules" ref="shopRefs" label-width="100px">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="shopForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="shopForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="shopForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="shopForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="CommodityVisible = false">取 消</el-button>
        <el-button type="primary" @click="xiugaiShop()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "goods",
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: null,
      tableData: [],
      CommodityVisible: false,
      shopForm: {
        goods_name: "",
        goods_price: "",
        goods_number: "",
        goods_weight: "",
      },
      shopRules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    getTableData() {
      this.$http.get("goods", { params: this.queryInfo }).then((res) => {
        if (res.data.meta.status !== 200) {
          return this.$message.error("数据加载失败");
        }
        this.tableData = res.data.data.goods;
        console.log(this.tableData);
        this.total = res.data.data.total;
      });
    },
    handleSizeChange(index) {
      this.queryInfo.pagesize = index;
      this.getTableData();
    },
    handleCurrentChange(index) {
      this.queryInfo.pagenum = index;
      this.getTableData();
    },
    deleteByid(row) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete(`goods/${row.goods_id}`).then((res) => {
            if (res.data.meta.status !== 200) {
              return this.$message.error("删除失败");
            }
            this.getTableData();
            this.$message.success("删除成功");
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addCommodity() {
      this.$router.push({ path: "addgoods" });
    },
    editCommodity(row) {
      this.$http.get(`goods/${row.goods_id}`).then((res) => {
        console.log(res);
        if (res.data.meta.status !== 200) {
          return this.$message.error("加载书架失败");
        }
        this.shopForm = res.data.data;
        this.CommodityVisible = true;
      });
    },
    closeShop() {
      this.$refs.shopRefs.resetFields();
    },
    xiugaiShop() {
      this.$refs.shopRefs.validate((validate) => {
        if (!validate) return;
        this.$http
          .put(`goods/${this.shopForm.goods_id}`, this.shopForm)
          .then((res) => {
            if (res.data.meta.status !== 200) {
              return this.$message.error("修改失败");
            }
            this.$message.success("修改成功");
            this.getTableData();
            this.CommodityVisible = false;
          });
      });
    },
  },
  mounted() {
    this.getTableData();
  },
};
</script>

<style scoped>
.el-breadcrumb {
  margin-bottom: 20px;
}
.el-row {
  margin-bottom: 10px;
}
.el-pagination {
  margin-top: 15px;
}
</style>
